﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>随机数</title>
    <script type="text/javascript" language="JavaScript">
        let count;
        function ganerateNum(){
            //生成一个从0到1之间的随机数,[0,1)
            //0.12341695793793
            //0.01234237288444
            //0.000000012316246682
            let i = parseInt(Math.random() * 9000 + 1000);
            let p = document.getElementById("t");
            p.value = i;
            initCount();
        }
        function initCount(){
            count = 0;
        }
        
        function checkNum(){
            let num = document.getElementById("t").value;
            let num1 = document.getElementById("t1").value;
            let check = "0123456789";
            for(let i = 0;i< num1.length;i++){
                let ran = num1.charAt(i);
                if(check.lastIndexOf(ran) < 0){
                    alert("本游戏只能输入数字");
                    return false;
                }
            }
            if(num1>num){
                alert("你猜的大了");
                count++;
            }else if(num1<num){
                alert("你猜的小了");
                count++;
            }else if(num1 === num){
                count++;
                alert("恭喜你，经过了"+count+"次，终于猜对了");
            }
        }

    </script>
</head>
<body>

<input type="password" id="t" size="10" maxlength="4" readonly="true">
<input type="button" value="生成随机数字！" onClick="ganerateNum()"><br>
<input type="text" id="t1" size="10" maxlength="4" align="center">
<input type="button" value="看看对了没有？" onClick="checkNum()">

</body>
</html>